package com.sixdog.regulate.service.impl;

import com.sixdog.regulate.bean.Activity;
import com.sixdog.regulate.bean.CmdResult;
import com.sixdog.regulate.cmdService.CmdUtils;
import com.sixdog.regulate.service.ActivityService;
import com.sixdog.regulate.service.KeycodeService;
import com.sixdog.regulate.utils.RegexUtil;
import com.sixdog.regulate.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private CmdUtils cmdUtils;
    @Autowired
    private KeycodeService keycodeService;


    //打开开放的Activity
    //adb -s 3431375099001IR exec-out am start -W -n com.taobao.taobao/com.taobao.tao.welcome.Welcome
    public CmdResult openActivity(String deviceSerial, Activity activity, String ...param){
        StringBuilder command = new StringBuilder().append("adb -s ").append(deviceSerial).append(" exec-out am start -W -n ").append(activity.getActivityName());
        //如果有参数则添加参数
        if (param!=null){
            command.append("-d ").append(param);
        }
        CmdResult cmdResult = cmdUtils.execute(command);
        return cmdResult;
    }

    //以最高权限打开Activity（需要手机root）
    @Override
    public CmdResult openActivityHighAuthority(String deviceSerial, Activity activity) {
        //adb -s R28M514XL6X shell "su -c 'am start  com.tencent.mobileqq/.activity.QQSettingSettingActivity'"
        StringBuilder command = new StringBuilder("adb -s ").append(deviceSerial).append(" shell \"su -c 'am start ")
                .append("-d ").append(activity.getDat()).append(" ")
                .append(activity.getActivityName()).append("'\"");
        CmdResult cmdResult = cmdUtils.execute(command);
        return cmdResult;
    }

    //捕获当前的Activity(需保持设备屏幕解锁)
    //adb shell dumpsys activity activities | findstr mResumedActivity
    @Override
    public Activity captureActivity(String deviceSerial){
        Activity activity = new Activity();
        StringBuilder command = new StringBuilder().append("adb -s ").append(deviceSerial).append(" exec-out dumpsys activity activities | findstr Intent");
        CmdResult cmdResult = cmdUtils.execute(command);
        // 避免越界异常
        String source = cmdResult.getLines().size() == 0 ? null : cmdResult.getLines().get(0);
        // 判断是否为null或者空串
        if (StringUtils.isBlank(source)){
            activity.setMessage("捕获失败，检查设备是否解锁屏幕");
            return activity;
        }
        //裁取得到activityName
        String activityName = RegexUtil.cutPattern("(?<=cmp=)\\S*",source);
        //截取得到activityPackage
        String activityPackage = RegexUtil.cutPattern("(?<=pkg=)\\S*",source);
        //截取得到dat
        String dat = RegexUtil.cutPattern("(?<=dat=)\\S*",source);

        activity.setActivityName(activityName);
        activity.setActivityPackage(activityPackage);
        activity.setDat(dat);
        return activity;
    }

    //刷新当前Activity(不好用,待优化)
    @Override
    public CmdResult refreshActivity(String deviceSerial) {
        Activity activity = captureActivity(deviceSerial);
        keycodeService.pressKey(deviceSerial,KeycodeService.KEYCODE_BACK);
        CmdResult cmdResult = openActivityHighAuthority(deviceSerial, activity);
        return cmdResult;
    }

    //禁用应用程序
    @Override
    public CmdResult disablePackage(String deviceSerial, String userId, String packageName) {
//        adb -s 80bd743d exec-out pm disable-user --user 901 com.taobao.taobao
        StringBuilder command = new StringBuilder().append("adb -s ").append(deviceSerial).append(" exec-out pm disable-user --user ")
                .append(userId).append(" ").append(packageName);
        CmdResult cmdResult = cmdUtils.execute(command);
        return cmdResult;
    }

    //启用应用程序（注意：启用不是打开）
    @Override
    public CmdResult enablePackage(String deviceSerial, String userId, String packageName) {
//        adb -s 80bd743d exec-out pm enable --user 903 com.taobao.taobao
        StringBuilder command = new StringBuilder().append("adb -s ").append(deviceSerial).append(" exec-out pm enable --user ")
                .append(userId).append(" ").append(packageName);
        CmdResult cmdResult = cmdUtils.execute(command);
        return cmdResult;
    }


}
